package com.supor.aiot.basket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.basket.R;
import com.supor.aiot.basket.api.BasketImpl;
import com.supor.aiot.basket.bean.IsCgIngredients;
import com.supor.aiot.basket.bean.IsMainIngredients;
import com.supor.aiot.basket.bean.IsTsIngredients;

import java.util.List;


public class CommonIngredientAdapter extends RecyclerView.Adapter<CommonIngredientAdapter.BasketHolder>{
    private List<IsMainIngredients> mList;
    private Context mContext;
    private IngredientSelectedListener listener;

    public CommonIngredientAdapter(List<IsMainIngredients> mList, Context mContext,IngredientSelectedListener listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.listener = listener;
    }

    @NonNull
    @Override
    public CommonIngredientAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_common_ingredients,parent, false);
        return new CommonIngredientAdapter.BasketHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {
        IsMainIngredients ingredients = mList.get(position);
        holder.cbForAll.setChecked((ingredients.getIsSelect()==0?false:true));
        holder.cbForAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                changeIngredientState(ingredients.getIngredientId()+"",b?1:0);
            }
        });
        if (!TextUtils.isEmpty(ingredients.getIngredientName()))holder.tvBasketTitle.setText(ingredients.getIngredientName());
    }

    @SuppressLint("AutoDispose")
    private void changeIngredientState(String id, int state){
        BasketImpl.getInstance().changeIngredientState(id,state).subscribe(result -> {
            if (result.isSuccess()) {
                listener.OnIngredientSelected();
            } else {
                Log.e("nail", "state error");
            }
        }, throwable -> {
            Log.e("nail", "state查询失败: " + throwable.getMessage());
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private CheckBox cbForAll;
        private TextView tvBasketTitle;

        public BasketHolder(@NonNull View itemView) {
            super(itemView);
             cbForAll = itemView.findViewById(R.id.cb_for_all);
             tvBasketTitle = itemView.findViewById(R.id.tv_ingredients_name);
        }
    }

}
